<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>数据管理 - 后台管理</title>
  <link rel="stylesheet" href="/static/base.css">
  <link rel="stylesheet" href="/static/components.css">
  <link rel="stylesheet" href="/static/pages.css">
  <link rel="stylesheet" href="/static/admin/admin.css">
</head>
<body>
  <div class="admin-layout">
    <!-- 顶部导航 -->
    <div class="admin-header">
      <h2>数据管理</h2>
      <div>
        <button class="btn" onclick="location.href='/'">返回前台</button>
        <button class="btn" onclick="logout()">退出</button>
      </div>
    </div>

    <!-- 主体内容 -->
    <div class="admin-main">
      <!-- 左侧导航（公共组件注入） -->
      <div class="admin-sidebar"></div>

      <!-- 右侧内容区域 -->
      <div class="admin-content">
        <div class="content-page active">
          <h3>数据管理</h3>
          <p class="page-description">管理股票信息、板块信息和板块关联数据</p>

          <div class="content-grid">
            <div class="content-card">
              <h4>股票信息管理</h4>
              <p>导入和管理股票基本信息</p>
              <button class="btn btn-primary" onclick="importStockInfo()">导入股票信息</button>
            </div>

            <div class="content-card">
              <h4>板块信息管理</h4>
              <p>导入和管理板块分类信息</p>
              <button class="btn btn-primary" onclick="importBlock()">导入板块信息</button>
            </div>

            <div class="content-card">
              <h4>板块关联管理</h4>
              <p>导入和管理股票与板块的关联关系</p>
              <button class="btn btn-primary" onclick="importBlockInfo()">导入板块关联</button>
            </div>

            <div class="content-card">
              <h4>数据统计</h4>
              <p>查看各表的数据统计信息</p>
              <button class="btn btn-primary" onclick="showDataStats()">查看统计</button>
            </div>
          </div>

          <!-- 数据统计面板 -->
          <div id="stats-panel" class="content-card" style="display: none;">
            <h4>数据统计</h4>
            <div id="stats-content"></div>
            <button class="btn" onclick="hideStats()">关闭</button>
          </div>
        </div>
      </div>
    </div>
  </div>

  <script src="/static/admin/components/sidebar.js"></script>
  <script>
    // 注入公共侧栏
    document.addEventListener('DOMContentLoaded', function(){ injectAdminSidebar('/admin/data-management'); });

    // 基本面管理相关函数
    async function importStockInfo() {
      try {
        const response = await fetch('/admin/api/fundamental/import-stockinfo', {
          method: 'POST'
        });
        const result = await response.json();
        
        if (result.success) {
          alert(`导入成功！\n成功: ${result.success_count} 条\n失败: ${result.error_count} 条`);
          if (result.failed_files && result.failed_files.length > 0) {
            // 失败的文件信息已记录
          }
        } else {
          alert('导入失败: ' + (result.error || '未知错误'));
        }
      } catch (error) {
        alert('导入失败: ' + error.message);
      }
    }

    async function importBlock() {
      try {
        const response = await fetch('/admin/api/fundamental/import-block', {
          method: 'POST'
        });
        const result = await response.json();
        
        if (result.success) {
          alert(`导入成功！\n成功: ${result.success_count} 条\n失败: ${result.error_count} 条`);
          if (result.failed_files && result.failed_files.length > 0) {
            // 失败的文件信息已记录
          }
        } else {
          alert('导入失败: ' + (result.error || '未知错误'));
        }
      } catch (error) {
        alert('导入失败: ' + error.message);
      }
    }

    async function importBlockInfo() {
      try {
        const response = await fetch('/admin/api/fundamental/import-blockinfo', {
          method: 'POST'
        });
        const result = await response.json();
        
        if (result.success) {
          alert(`导入成功！\n成功: ${result.success_count} 条\n失败: ${result.error_count} 条`);
          if (result.failed_files && result.failed_files.length > 0) {
            // 失败的文件信息已记录
          }
        } else {
          alert('导入失败: ' + (result.error || '未知错误'));
        }
      } catch (error) {
        alert('导入失败: ' + error.message);
      }
    }

    async function showDataStats() {
      try {
        const response = await fetch('/admin/api/fundamental/stats');
        const result = await response.json();
        
        if (result.success) {
          const statsContent = document.getElementById('stats-content');
          statsContent.innerHTML = `
            <div style="display:grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap:16px; margin-bottom:16px;">
              <div style="padding:16px; background:#f8f9fa; border-radius:8px;">
                <div style="font-size:24px; font-weight:bold; color:#007bff;">${result.stockinfo_count}</div>
                <div style="color:#6c757d;">StockInfo记录数</div>
              </div>
              <div style="padding:16px; background:#f8f9fa; border-radius:8px;">
                <div style="font-size:24px; font-weight:bold; color:#28a745;">${result.block_count}</div>
                <div style="color:#6c757d;">Block记录数</div>
              </div>
              <div style="padding:16px; background:#f8f9fa; border-radius:8px;">
                <div style="font-size:24px; font-weight:bold; color:#ffc107;">${result.blockinfo_count}</div>
                <div style="color:#6c757d;">BlockInfo记录数</div>
              </div>
            </div>
          `;
          document.getElementById('stats-panel').style.display = 'block';
        } else {
          alert('获取统计信息失败: ' + (result.error || '未知错误'));
        }
      } catch (error) {
        alert('获取统计信息失败: ' + error.message);
      }
    }

    function hideStats() {
      document.getElementById('stats-panel').style.display = 'none';
    }

    // 退出登录
    async function logout(){
      try{ 
        await fetch('/admin/logout', {method:'POST'}); 
        location.href='/admin/login'; 
      }catch(e){ 
        location.href='/admin/login'; 
      }
    }
  </script>
</body>
</html>
